package com.ekingstar.framework.action;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.struts2.config.Action;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.ekingstar.framework.domain.User;
import com.ekingstar.framework.service.IUserService;
import com.ekingstar.framework.service.UserExistException;

@SuppressWarnings({"serial","unchecked"})
@Action(name="addOrUpdate",namespace="/user")
@Results({
		 @Result(name="list",value="user",type=ServletActionRedirectResult.class,params={"namespace","/user"})
		})
public class AddOrUpdateUserAction extends ActionSupport implements Preparable {
	private IUserService userService;
	
	private Map userTypes;
	

	public Map getUserTypes() {
		return userTypes;
	}

	public void setUserTypes(Map userTypes) {
		this.userTypes = userTypes;
	}

	

	private User user;
	
	public String input(){
		if (user != null && user.getId() !=null){
			user = userService.viewUser(user.getId());
		}
		
		return "input";
	}
	
	@Validations(
			requiredStrings={
					@RequiredStringValidator(message="用户名必填",fieldName="user.name"),
					@RequiredStringValidator(message="姓名必填",fieldName="user.userInfo.name"),
					@RequiredStringValidator(message="密码必填",fieldName="user.pwd")},
					
			regexFields={
					@RegexFieldValidator(message="邮箱格式不正确",fieldName="user.userInfo.email",expression="\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"),
					@RegexFieldValidator(message="密码不安全(6-20位)",fieldName="user.pwd",expression="[0-9a-zA-Z]{6,20}")
					}
			)
	public String addOrUpdate(){
		try{
			if (user != null && user.getId() != null && !user.getId().trim().equals("")){
				userService.updateUser(user);
			}else {
				userService.addUser(user);
			}
		}catch(UserExistException e){
			this.addFieldError(ERROR,e.getMessage());
			return "input";
		}
		
		return "list";
	}
	
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	public void prepare() throws Exception {

		//universitys =  CodeUtil.getInctance().getCodeNames("dm_gx");
		Map tmp = new HashMap();
		tmp.put("", "----请选择----");
		if (userTypes == null) {
			userTypes = new LinkedHashMap();
			User.Type[] uts = User.Type.values();
			for (int i = 0 ;i  < uts.length ; i++){
				userTypes.put(uts[i].name(), uts[i].toString());
			}
		}
		
	}


	
}
